<?php
	/* l��ѡ����ݿ� */
	$dbSelected = "" ;
	$dbHost = "" ;
	date_default_timezone_set ('Asia/Shanghai');

	function mysql_init ( $host, $db, $charset, $user = "db", $pass = "db" )
	{
	  GLOBAL $dbSelected ;
		GLOBAL $dbHost ;
	  $dbHost = $host ;
		$dbSelected = $db ;
   	mysql_connect ($host, $user, $pass) or die("Could not connect $host : " . mysql_error () ) ;
	  mysql_select_db ( $db ) or die( "Could not select database $db : " . mysql_error () ) ;
		mysql_query ( "SET NAMES $charset" ) or die ( "Could not use names $charset : " . mysql_error () ) ;
	}

	function mysql_host ( $host ) {
	  GLOBAL $dbHost ;
	  $dbHost = $host ;
	}

	function mysql_log ( $message ) {
    $today = strftime ( "%y%m%d" ) ;
		$handle = fopen( "/tmp/$dbHost.$dbSelected.$today.log", 'a' ) ;
		if ( $handle !== false ) {
			fprintf ( $handle, "%s\r\n", $message ) ;
			fclose( $handle );
		}
	}

	function mysql_exec ( $_query ) {
	  GLOBAL $dbSelected ;
		GLOBAL $dbHost ;
	  if ( $dbSelected == "" )
    	die ( "No database selected\r\n" ) ;

	  $tl = microtime ( true ) ;
		$result = mysql_query ( $_query ) or die ( "MySQL error #" . mysql_errno() . "," . mysql_error () . ", <pre>$_query</pre>" ) ;
	  $tl = microtime ( true ) - $tl ;

	if($log){
    $today = strftime ( "%y%m%d" ) ;
		$handle = fopen( "/tmp/$dbHost.$dbSelected.$today.log", 'a' ) ;
		if ( $handle !== false ) {
		    $currTime = strftime ( "%T" ) ;
			fprintf ( $handle, "%s %0.4f\t%s\r\n", $currTime, $tl, $_query ) ;
			fclose( $handle );
		}
	}

		return $result ;
	}

  function fnGetValue ( $avTable, $avClause, $avField, $abSlashes = 0 ) {
//	echo "    $avClause = ". $avClause. "    ";
    $tmpQuery = "SELECT " . $avField . " FROM " . $avTable ;
		if ( $avClause != '' )
		  $tmpQuery .= " WHERE $avClause" ;
//	print "<sql>$tmpQuery</sql>\r\n" ;
    $tmpResult = mysql_exec ( $tmpQuery ) ;
    if ( $line = mysql_fetch_row ($tmpResult) )
      $ret = $line[0] ;
    else
      $ret = "" ;
    mysql_free_result ($tmpResult) ;
    if ( ! $abSlashes )
      return stripslashes ($ret) ;
    else
      return $ret ;
  }

  function fnGetArray ( $avTable, $avClause, $avField ) {
    $tmpQuery = "SELECT " . $avField . " FROM " . $avTable ;
		if ( $avClause != '' )
		  $tmpQuery .= " WHERE $avClause" ;
    // print "<sql>$tmpQuery</sql>\r\n" ;
    $res = mysql_exec ( $tmpQuery ) ;
		$ret = array () ;
		while ( $row = mysql_fetch_row ( $res ) ) {
		  $ret[] = $row[0] ;
		}
		mysql_free_result ( $res ) ;
    return $ret ;
  }

  function fnGetObject ( $avTable, $avClause, $avFields ) {
    $tmpQuery = "SELECT " . $avFields . " FROM " . $avTable ;
		if ( $avClause != '' )
		  $tmpQuery .= " WHERE $avClause" ;
    // print "<sql>$tmpQuery</sql>\r\n" ;
    $res = mysql_exec ( $tmpQuery ) ;
		$ret = null ;
		if ( $row = mysql_fetch_object ( $res ) ) {
		  $ret = $row ;
		}
		mysql_free_result ( $res ) ;
    return $ret ;
  }

  function fnFillList ( $asSQL, $avVal, $avMultiple, $avNullable ) {
    $selected = 0 ;
    $result = mysql_exec ( $asSQL ) ;
    while ( $line = mysql_fetch_row ($result )) {
      print '<option value=' . $line[0] ;
      if ( $avMultiple ) {
        if ( (($line[0] + 0) & ($avVal + 0)) != 0 )
          print " selected" ;
      } elseif ( $line[0] == $avVal ) {
        print " selected" ;
        $selected = 1 ;
      }
      print '>' . $line[1] . "\r\n" ;
    }
    mysql_free_result ($result) ;
    if ( ! $avMultiple && $avNullable != 0 ) {
      print "<option value=-1" ;
			if ( $selected != 1 )
			  print " selected" ;
			print ">-��ѡ��-" ;
		}
    print "</select>\r\n" ;
  }
?>
